$(function() {
  $('button.enable').click(function() {
    var data = {};
    data.data = {};
    data.op = 'enable';
    data.data.id = $(this).attr('data-id');
    $.post('/collection/op/', data, function(data) {
      if (data.code != '200') alert('设置失败!');
    }, 'json')
  });

  $('button.disable').click(function() {
    var data = {};
    data.data = {};
    data.op = 'disable';
    data.data.id = $(this).attr('data-id');
    $.post('/collection/op/', data, function(data) {
      if (data.code != '200') alert('设置失败!');
    }, 'json')
  });

  $('button.delete').click(function() {
    var row = $(this).parent().parent();
    var data = {};
    data.data = {};
    data.op = 'delete';
    data.data.id = $(this).attr('data-id');
    if (confirm('确实要删除吗？')) {
      $.post('/collection/op/', data, function(data) {
        if (data.code != '200') alert('删除失败!');
        else row.fadeOut('slow');
      }, 'json')
    }
  });

  $('button.config').click(function() {
    window.location.href = '/collection/info/?id='+$(this).attr('data-id');
  });


  $('a.sort_op').click(function() {
    var data = {};
    data.op = $(this).attr('data-op');
    data.collection_id = $(this).attr('data-collection-id');
    $.post('/collection/sort_op', data, function(ret) {
      if (ret.code == 200) {
        $('.sort-'+data.collection_id).html(ret.sort);
      } else {
        alert('操作失败!');
      }
    }, 'json');
  });
});
